﻿namespace Tanmia.Basis.Prism
{
    using System;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Controls.Primitives;

    public class BaseWindow : Window
    {
        #region Private Fields
        private double height;
        private double width;
        private double top;
        private double left;
        #endregion

        public BaseWindow()
        {
            AddHandler(MouseDoubleClickEvent, new MouseButtonEventHandler(OnWindowMouseDoubleClick));
            AddHandler(Mouse.MouseDownEvent, new MouseButtonEventHandler(OnWindowMouseDownEvent));
            AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(OnWindowButtonClick));
            AddHandler(ToggleButton.CheckedEvent, new RoutedEventHandler(OnWindowToggleButtonChecked));
            AddHandler(ToggleButton.UncheckedEvent, new RoutedEventHandler(OnWindowToggleButtonChecked));
            AddHandler(LoadedEvent, new RoutedEventHandler(OnWindowLoaded));
        }

        protected virtual void OnWindowMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement fe = GetControl(e.OriginalSource);
            if (fe.Name == "CaptionBorder")
                switch (WindowState)
                {
                    case System.Windows.WindowState.Normal:
                        WindowState = System.Windows.WindowState.Maximized;
                        break;
                    case System.Windows.WindowState.Maximized:
                        WindowState = System.Windows.WindowState.Normal;
                        break;
                }
        }

        protected virtual void OnWindowMouseDownEvent(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement fe = GetControl(e.OriginalSource);
            if (fe.Name == "CaptionBorder")
                if (e.ChangedButton == MouseButton.Left)
                    DragMove();
        }

        protected virtual void OnWindowButtonClick(object sender, RoutedEventArgs e)
        {
            FrameworkElement fe = e.OriginalSource as FrameworkElement;
            if (fe != null)
                if (fe.Name == "MinButton")
                    WindowState = System.Windows.WindowState.Minimized;
                else if (fe.Name == "CloseButton")
                    Close();
        }

        protected virtual void OnWindowToggleButtonChecked(object sender, RoutedEventArgs e)
        {
            if ((bool)((ToggleButton)e.OriginalSource).IsChecked)
            {
                WindowState = WindowState.Maximized;
            }
            else
            {
                this.Height = height;
                this.Width = width;
                this.Top = top;
                this.Left = left;

                WindowState = WindowState.Normal;
            }
        }

        protected virtual void OnWindowLoaded(object sender, RoutedEventArgs e)
        {
            height = this.ActualHeight;
            width = this.ActualWidth;
            top = this.Top;
            left = this.Left;
        }

        protected FrameworkElement GetControl(object original)
        {
            if (original == null)
                throw new ArgumentNullException("original");

            if (original as FrameworkElement == null)
                throw new InvalidOperationException("An original control is not FrameworkElement.");

            return original as FrameworkElement;
        }
    }
}
